package com.dcy.znoa.controller.publiz;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.canteen.SelectCanteenModel;
import com.dcy.znoa.controller.publiz.model.canteenMenu.AddCanteenMenuModel;
import com.dcy.znoa.controller.publiz.model.canteenMenu.SelectCanteenMenuModel;
import com.dcy.znoa.controller.publiz.model.canteenMenu.SelectCanteenMenuOneDayModel;
import com.dcy.znoa.controller.publiz.model.canteenMenu.UpdateCanteenMenuModel;
import com.dcy.znoa.domain.publiz.Canteen;
import com.dcy.znoa.domain.publiz.CanteenMenu;
import com.dcy.znoa.service.publiz.CanteenMenuService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

import static com.dcy.znoa.common.util.pojo.JsonCommon.*;

/**
 * @author pengyu
 */
//食堂菜谱 controller
@Controller
@RequestMapping("canteenMenu")
public class CanteenMenuController extends BaseController {
    @Autowired
    CanteenMenuService canteenMenuService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectOneDay")
    public JsonCommon selectOneDay(@Valid SelectCanteenMenuOneDayModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        return canteenMenuService.selectOneDay(model);
//        List list = canteenService.selectList(model);
//        if(list.isEmpty()){
//            return new JsonForList(NOT_FOUND_CODE);
//        }else{
//            return new JsonForList(list);
//        }
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(@Valid SelectCanteenMenuModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonForList("404",bindingResult.getFieldError().getDefaultMessage());
        }
        return canteenMenuService.selectList(model);
//        List list = canteenService.selectList(model);
//        if(list.isEmpty()){
//            return new JsonForList(NOT_FOUND_CODE);
//        }else{
//            return new JsonForList(list);
//        }
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddCanteenMenuModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setCreateOperId(getCurrentUser().getOperId());
        CanteenMenu cp = new CanteenMenu();
        BeanUtils.copyProperties(cp, model);
        Integer result =  canteenMenuService.insert(cp);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(INSERT_ERROR_CODE);
        }
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateCanteenMenuModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        CanteenMenu cp = new CanteenMenu();
        BeanUtils.copyProperties(cp, model);
        Integer result =  canteenMenuService.updateById(cp);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(UPDATE_ERROR_CODE);
        }
    }


    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/deleteById")
    public JsonCommon deleteById( Long cmId) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (cmId == null){
            return  new JsonCommon("404","删除时主键ID不能为空");
        }
        Integer result =  canteenMenuService.deleteById(cmId);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(DELETE_ERROR_CODE);
        }
    }


}
